// miniprogram/pages/main/main.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 'tme'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const ui = wx.getStorageSync('userinfo')
    console.log("start: ", ui)

    app.globalData.userInfo = ui

  },
  onChange(event) {
    console.log(event.detail)
    this.setData({
      active: event.detail,
    });

    let that = this
    wx.navigateTo({
      url: "/pages/" + that.data.active + "/" + that.data.active
    })
  },
  onGotUserInfo: function (e) {
    const that = this
    wx.cloud.callFunction({
      name: "login",
      success: res => {
        console.log("云函数调用成功:", res)
        that.setData({
          userInfo: res.result
        })
        wx.setStorageSync("userinfo", that.data.userInfo)

        app.globalData.userInfo = that.data.userInfo

        let role = e.currentTarget.dataset.role
        this.jump(role)
      },
      fail: res => {
        console.log("云函数调用失败")
      }
    })
  },
  onNavigateTo: function (e) {
    let role = e.currentTarget.dataset.role
    console.log(role)
    this.jump(role)
  },
  jump: function (role) {
    if (role == "student") {
      wx.navigateTo({
        url: '/pages/student/student',
      })
    } else if (role == "teacher") {
      wx.navigateTo({
        url: '/pages/teacher/teacher'
      })
    }
  }
})